package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AuthInfoBo;
import com.cskaoyan.bean.bo.ProfilePasswordBo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.pojo.admin.*;
import com.cskaoyan.bean.vo.AdminDashboardVo;
import com.cskaoyan.bean.vo.AuthInfoVo;
import com.cskaoyan.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author: lds
 * @clsss: AdminServiceImpl
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminRoleMapper adminRoleMapper;
    @Autowired
    AdminPermissionMapper adminPermissionMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    AuthorPermissionMapper authorPermissionMapper;


    /**
     * @author: lds
     * @title: queryAdmin
     * @description: 查询登录者（管理员）的信息,返回给auth/info页面
     * @param: [authInfoBo]
     * @return: com.cskaoyan.bean.vo.AuthInfoVo
     */
    @Override
    public AuthInfoVo queryAdmin(AuthInfoBo authInfoBo) {


        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        String username = authInfoBo.getUsername();
        String password = authInfoBo.getPassword();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        criteria.andDeletedEqualTo(false);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Admin admin = admins.get(0);
        Integer[] roleIds = admin.getRoleIds();

        List<Integer> roleIdList = Arrays.asList(roleIds);

        AdminRoleExample adminRoleExample = new AdminRoleExample();
        AdminRoleExample.Criteria criteriaAdminRole = adminRoleExample.createCriteria();

        criteriaAdminRole.andIdIn(roleIdList);
        criteria.andDeletedEqualTo(false);
        List<AdminRole> adminRoles = adminRoleMapper.selectByExample(adminRoleExample);

        ArrayList<String> roles = new ArrayList<>();
        for (AdminRole adminRole : adminRoles) {
            roles.add(adminRole.getName());
        }

        AdminPermissionExample adminPermissionExample = new AdminPermissionExample();
        AdminPermissionExample.Criteria criteriaAdminPermissionExample = adminPermissionExample.createCriteria();
        criteriaAdminPermissionExample.andRoleIdIn(roleIdList);
        criteriaAdminPermissionExample.andDeletedEqualTo(false);
        List<AdminPermission> adminPermissions = adminPermissionMapper.selectByExample(adminPermissionExample);
        ArrayList<String> perms = new ArrayList<>();
        for (AdminPermission adminPermission : adminPermissions) {
            AuthorPermissionExample authorPermissionExample = new AuthorPermissionExample();
            AuthorPermissionExample.Criteria criteria1 = authorPermissionExample.createCriteria();
            criteria1.andPermissionEqualTo(adminPermission.getPermission());

            List<AuthorPermission> authorPermissions = authorPermissionMapper.selectByExample(authorPermissionExample);
            perms.add(authorPermissions.get(0).getApi());
        }

        AuthInfoVo authInfoVo = new AuthInfoVo();
        authInfoVo.setName(username);
        authInfoVo.setAvatar(admin.getAvatar());
        authInfoVo.setPerms(perms);
        authInfoVo.setRoles(roles);

        return authInfoVo;
    }

    /**
     * @author: lds
     * @title: queryInfoMessage
     * @description: 查询 user人数 ，goods数量 ，goodsproduct 数量 ，order数量
     * @param: []
     * @return: com.cskaoyan.bean.vo.AdminDashboardVo
     */
    @Override
    public AdminDashboardVo queryInfoMessage() {
        int userNum = 0;
        int goodsNum = 0;
        int orderNum = 0;
        int goodsProductNum = 0;
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        try {
            userNum = (int) userMapper.countByExample(userExample);
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
            criteria1.andDeletedEqualTo(false);
            goodsNum = (int) goodsMapper.countByExample(goodsExample);
            OrderExample orderExample = new OrderExample();
            OrderExample.Criteria criteria2 = orderExample.createCriteria();
            criteria2.andDeletedEqualTo(false);
            orderNum = (int) orderMapper.countByExample(orderExample);
            GoodsProductExample goodsProductExample = new GoodsProductExample();
            GoodsProductExample.Criteria criteria3 = goodsProductExample.createCriteria();
            criteria3.andDeletedEqualTo(false);
            goodsProductNum = (int) goodsProductMapper.countByExample(goodsProductExample);
        } catch (Exception e) {
            return null;
        }


//        System.out.print("userNum: " +userNum);
//        System.out.print("goodsNum: "+goodsNum);
//        System.out.print("orderNum: "+orderNum);
//        System.out.print("goodsProductNum: "+goodsProductNum);
        AdminDashboardVo adminDashboardVo = new AdminDashboardVo();
        adminDashboardVo.setProductTotal(goodsProductNum);
        adminDashboardVo.setUserTotal(userNum);
        adminDashboardVo.setGoodsTotal(goodsNum);
        adminDashboardVo.setOrderTotal(orderNum);
        return adminDashboardVo;
    }

    @Override
    public Integer changePassword(Admin admin, ProfilePasswordBo profilePasswordBo) {

        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andIdEqualTo(admin.getId());
        Admin admin1 = new Admin();
        admin1.setPassword(profilePasswordBo.getNewPassword());
        int i = adminMapper.updateByExampleSelective(admin1, adminExample);
        return i;
    }

    @Override
    public Integer addPwd(String email, String verificationCode) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andEmailEqualTo(email);
        Admin admin1 = new Admin();
        admin1.setPwd(verificationCode);
        int i = adminMapper.updateByExampleSelective(admin1, adminExample);
        return i;
    }

    @Override
    public Integer changeIpAndLoginTime(Admin admin, String host, Date date) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername());
        Admin admin1 = new Admin();
        admin1.setLastLoginIp(host);
        admin1.setLastLoginTime(date);
        int i = adminMapper.updateByExampleSelective(admin1, adminExample);
        return i;
    }

    @Override
    public Admin queryAdminExit(String email, String pwd) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andEmailEqualTo(email);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        criteria.andPwdEqualTo(pwd);
        List<Admin> admins1 = adminMapper.selectByExample(adminExample);
        Admin admin = admins.get(0);
        if (admins.size() == 1&&admins1.size()==0) {//验证码错误

            admin.setPwd(null);
            return admin;
        }else if (admins.size() == 1&&admins1.size()==1){
            Admin admin2 =admin;
            admin2.setPwd(null);
            admin.setPwd("123");
            int i = adminMapper.updateByExample(admin2, adminExample);
            return admin;
        }else {
            return null;
        }
    }

    @Override
    public Integer removePwd(Admin admin) {
        Integer id = admin.getId();
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andIdEqualTo(id);

        admin.setPwd(null);
        int i = adminMapper.updateByExample(admin, adminExample);
        return i;
    }
}
